#!/bin/bash

# MAX6675测试脚本

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

# 检查驱动是否加载
check_driver() {
    if lsmod | grep -q "max6675"; then
        echo -e "${GREEN}MAX6675 driver is loaded${NC}"
        return 0
    else
        echo -e "${RED}MAX6675 driver is not loaded${NC}"
        return 1
    fi
}

# 检查sysfs接口
check_sysfs() {
    local paths=(
        "/sys/bus/iio/devices/iio:device0/in_temp_raw"
        "/sys/bus/iio/devices/iio:device0/temperature_unit"
        "/sys/bus/iio/devices/iio:device0/sampling_frequency"
    )

    for path in "${paths[@]}"; do
        if [ -f "$path" ]; then
            echo -e "${GREEN}Found $path${NC}"
        else
            echo -e "${RED}Missing $path${NC}"
            return 1
        fi
    done
    return 0
}

# 编译测试程序
compile_test() {
    echo "Compiling test program..."
    gcc -o max6675_test max6675_test.c
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}Compilation successful${NC}"
        return 0
    else
        echo -e "${RED}Compilation failed${NC}"
        return 1
    fi
}

# 运行测试
run_test() {
    echo "Running tests..."
    ./max6675_test
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}Tests completed successfully${NC}"
        return 0
    else
        echo -e "${RED}Tests failed${NC}"
        return 1
    fi
}

# 主测试流程
main() {
    echo "Starting MAX6675 tests..."

    # 检查驱动
    check_driver || exit 1

    # 检查sysfs接口
    check_sysfs || exit 1

    # 编译测试程序
    compile_test || exit 1

    # 运行测试
    run_test || exit 1

    echo "All tests completed"
}

# 运行主测试流程
main 